package com.laowang.project.monitor.mapper;

import java.util.Date;
import java.util.List;

import com.laowang.common.utils.StringUtils;
import com.laowang.project.monitor.domain.SysJobLog;
import org.apache.ibatis.annotations.Update;
import tk.mybatis.mapper.additional.idlist.DeleteByIdListMapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

/**
 * 调度任务日志信息 数据层
 * 
 * @author laowang
 */
public interface SysJobLogMapper extends Mapper<SysJobLog>, DeleteByIdListMapper<SysJobLog, Long>
{
    /**
     * 获取quartz调度器日志的计划任务
     * 
     * @param jobLog 调度日志信息
     * @return 调度任务日志集合
     */
    default List<SysJobLog> selectJobLogList(SysJobLog jobLog) {
        Example example = new Example(SysJobLog.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(jobLog.getJobName())) {
            criteria.andLike("jobName", "%" + jobLog.getJobName() + "%");
        }
        if (StringUtils.isNotEmpty(jobLog.getJobGroup())) {
            criteria.andEqualTo("jobGroup", jobLog.getJobGroup());
        }
        if (StringUtils.isNotEmpty(jobLog.getStatus())) {
            criteria.andEqualTo("status", jobLog.getStatus());
        }
        if (StringUtils.isNotEmpty(jobLog.getInvokeTarget())) {
            criteria.andLike("invokeTarget", "%" + jobLog.getInvokeTarget() + "%");
        }
        if (StringUtils.isNotEmpty(jobLog.getBeginTime())) {
            criteria.andCondition("date_format(create_time, '%y%m%d') >= date_format('" + jobLog.getBeginTime() + "', '%y%m%d')");
        }
        if (StringUtils.isNotEmpty(jobLog.getEndTime())) {
            criteria.andCondition("date_format(create_time, '%y%m%d') <= date_format('" + jobLog.getEndTime() + "', '%y%m%d')");
        }
        return selectByExample(example);
    }

    /**
     * 新增任务日志
     * 
     * @param jobLog 调度日志信息
     * @return 结果
     */
    default int insertJobLog(SysJobLog jobLog) {
        jobLog.setCreateTime(new Date());
        return insertSelective(jobLog);
    }

    /**
     * 清空任务日志
     */
    @Update("truncate table sys_job_log")
    void cleanJobLog();
}
